package com.vdg.lockapp;

import group.pals.android.lib.ui.lockpattern.LockPatternActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.vdg.data.SettingData;
import com.vdg.data.SettingData.LOCK_TYPE;
import com.vdg.lockapp.PassCodeActivity.PASS_CODE_ACTION_TYPE;
import com.vdg.lockapp.service.LockService;
import com.vdg.lockapp.service.LockService.LocalBinder;
import com.vdg.util.Utilities;

public class HackActivity extends Activity {
	private LockService mService;
	private static final int REQ_ENTER_PATTERN = 2;
	private static final int REQ_ENTER_PASSCODE = 1;
	private Intent mIntent;
	private static final String TAG = "HackActivity";
	private AlertDialog mCrashDialog;
	private SettingData mSettingData;
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		// required for Sherlock's invalidateOptionsMenu */
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mService = binder.getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mCrashDialog = new Builder(this).create();
		// mCrashDialog.setIcon(android.R.drawable.ic_)
		mCrashDialog
				.setMessage("The application Test (process.com.test) has stoped unexpectedly. please try again.");
		mCrashDialog.setCancelable(false);
		mIntent = getIntent();
		if (mIntent != null) {
			String appName = mIntent
					.getStringExtra(LockService.APP_LOCK_PACKAGE_NAME);
			mCrashDialog.setMessage(getResources().getString(R.string.fake_crash,Utilities.getAppName(HackActivity.this, appName)));
		}
		mCrashDialog.setButton(getResources().getString(R.string.ok), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				HackActivity.this.finish();
				gotoHome();
			}
		});

		binService();
		startLockActivity();
	}

	private void startLockActivity() {
		mSettingData = SettingData.getInstance(this);
		if (mSettingData.getLockType().equals(LOCK_TYPE.PASS_CODE)) {
			startLockPasscode();
		} else {
			if (mSettingData.getLockType().equals(LOCK_TYPE.FAKE_FORCE_CLOSE)) {
				startFakeForceClose();
			} else {
				startLockPattern();
			}
		}
	}

	private void binService() {
		Intent serviceIntent = new Intent(this, LockService.class);
		bindService(serviceIntent, mServiceConnection, BIND_AUTO_CREATE);
	}

	private void startLockPattern() {
		Intent intent = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN,
				null, this, LockPatternActivity.class);
		intent.putExtra(LockPatternActivity.EXTRA_PATTERN,
				Utilities.loadPatternFromSharePreferences(this));
		startActivityForResult(intent, REQ_ENTER_PATTERN);
	}

	private void startLockPasscode() {
		Intent intent = new Intent(this, PassCodeActivity.class);
		intent.putExtra(LockService.APP_LOCK_PACKAGE_NAME, getIntent()
				.getStringExtra(LockService.APP_LOCK_PACKAGE_NAME));
		intent.putExtra(PassCodeActivity.PASS_CODE_ACTION, getIntent()
				.getStringExtra(PassCodeActivity.PASS_CODE_ACTION));
		startActivityForResult(intent, REQ_ENTER_PASSCODE);
	}

	private void startFakeForceClose() {
		mCrashDialog.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Log.v(TAG, "hack0 " + requestCode + "--------" + REQ_ENTER_PATTERN
				+ "---------" + resultCode);
		switch (requestCode) {
		case REQ_ENTER_PATTERN: {
			/*
			 * NOTE that there are 4 possible result codes!!!
			 */
			switch (resultCode) {
			case RESULT_OK:
				// The user passed

				Log.v(TAG, "hack");
				if (mIntent != null) {
					mService.unlockApp(mIntent
							.getStringExtra(LockService.APP_LOCK_PACKAGE_NAME));
					Log.v(TAG,
							"hack 1 "
									+ mIntent
											.getStringExtra(LockService.APP_LOCK_PACKAGE_NAME));
				}
				Log.v(TAG, "hack 2");
				break;
			case RESULT_CANCELED:
				Log.v(TAG, "hack3");
				// The user cancelled the task
				break;
			case LockPatternActivity.RESULT_FAILED:
				Log.v(TAG, "hack4");
				// The user failed to enter the pattern
				break;
			case LockPatternActivity.RESULT_FORGOT_PATTERN:
				Log.v(TAG, "hack5");
				// The user forgot the pattern and invoked your recovery
				// Activity.
				break;
			}

			/*
			 * In any case, there's always a key EXTRA_RETRY_COUNT, which holds
			 * the number of tries that the user did.
			 */
			// int retryCount = data.getIntExtra(
			// LockPatternActivity.EXTRA_RETRY_COUNT, 0);

			break;
		}// REQ_ENTER_PATTERN

		case REQ_ENTER_PASSCODE: {
			/*
			 * NOTE that there are 4 possible result codes!!!
			 */
			switch (resultCode) {
			case RESULT_OK:
				if (mIntent != null) {
					mService.unlockApp(mIntent
							.getStringExtra(LockService.APP_LOCK_PACKAGE_NAME));
				}
				Log.v(TAG, "hack 2");
				break;
			case RESULT_CANCELED:
				Log.v(TAG, "hack3");
				gotoHome();
				break;
			case LockPatternActivity.RESULT_FAILED:
				Log.v(TAG, "hack4");
				// The user failed to enter the pattern
				break;
			case LockPatternActivity.RESULT_FORGOT_PATTERN:
				Log.v(TAG, "hack5");
				// The user forgot the pattern and invoked your recovery
				// Activity.
				break;
			}

			/*
			 * In any case, there's always a key EXTRA_RETRY_COUNT, which holds
			 * the number of tries that the user did.
			 */
			// int retryCount = data.getIntExtra(
			// LockPatternActivity.EXTRA_RETRY_COUNT, 0);

			break;
		}// REQ_ENTER_PATTERN
		}
		HackActivity.this.finish();
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unbindService(mServiceConnection);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// mCrashDialog.dismiss();
		super.onPause();
	}

	private void gotoHome() {
		Intent homeIntent = new Intent(Intent.ACTION_MAIN);
		homeIntent.addCategory(Intent.CATEGORY_HOME);
		homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(homeIntent);
	}
}
